/*
 * 
 */
package com.yuanmei.stockeyes.charts;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnTouchListener;

import com.tictactec.ta.lib.Core;
import com.tictactec.ta.lib.MInteger;
import com.tictactec.ta.lib.RetCode;
import com.yuanmei.stockeyes.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.limc.androidcharts.entity.DateValueEntity;
import cn.limc.androidcharts.entity.LineEntity;
import cn.limc.androidcharts.event.IDisplayCursorListener;
import cn.limc.androidcharts.view.SlipMinusLineChart;

//import android.util.Log;

public class TalibAdoscChart implements ITalibChart {
    private final Core mTaLibCore = new Core();
    private Context mContext;
    private SlipMinusLineChart mAdoscChartView;
    private static final String TAG = TalibAdoscChart.class.getName();

    private List<DateValueEntity> mAdoscDateValueList;

    private LineEntity<DateValueEntity> mAdoscLineEntity;

    private int mAdoscFastPeriod = 3;
    private String mAdoscFastPeriodName;

    private int mAdoscSlowPeriod = 10;
    private String mAdoscSlowPeriodName;

    private int mTaIdxDataSetsBegin;
    private int mTaIdxDataSetsNumber;

    public TalibAdoscChart(Context context) {
        mAdoscChartView = new SlipMinusLineChart(context);
        TalibChartFactory.commonInitGridChart(mAdoscChartView);
        TalibChartFactory.commonInitSlipLineChart(mAdoscChartView);

        mContext          =  mAdoscChartView.getContext();
        mAdoscFastPeriodName = mContext.getString(R.string.param_adosc_fast_period);
        mAdoscSlowPeriodName = mContext.getString(R.string.param_adosc_slow_period);
    }

    @Override
    public List<Map<String, Object>> getTaParamsList() {
        List<Map<String, Object>> outParamsList = new ArrayList<Map<String, Object>>();

        Map<String, Object> item = new HashMap<String, Object>();
        item.put(ITalibChart.ITaParamNameKey,  mAdoscFastPeriodName);
        item.put(ITalibChart.ITaParamValueKey, mAdoscFastPeriod);
        outParamsList.add(item);

        item = new HashMap<String, Object>();
        item.put(ITalibChart.ITaParamNameKey,  mAdoscSlowPeriodName);
        item.put(ITalibChart.ITaParamValueKey, mAdoscSlowPeriod);
        outParamsList.add(item);

        return outParamsList;
    }

    @Override
    public void setTaParams(Hashtable<String, Integer> inParamMaps) {
        if (inParamMaps.containsKey(mAdoscFastPeriodName)
              && inParamMaps.containsKey(mAdoscSlowPeriodName)
                ) {
            mAdoscFastPeriod = inParamMaps.get(mAdoscFastPeriodName);
            mAdoscSlowPeriod = inParamMaps.get(mAdoscSlowPeriodName);
        }
    }

    @Override
    public boolean reloadData(int dataLen,
                              float[] openPrices,
                              float[] highPrices,
                              float[] lowPrices,
                              float[] closePrices,
                              float[] volumes,
                              float[] amounts,
                              int[] dates) {
        MInteger outBegIdx    = new MInteger();
        MInteger outNBElement = new MInteger();
        double[] outAdosc     = new double[dataLen];

        RetCode retCode = mTaLibCore.adOsc(0, closePrices.length - 1,
                highPrices, lowPrices, closePrices, volumes,
                mAdoscFastPeriod, mAdoscSlowPeriod,
                outBegIdx, outNBElement, outAdosc);
        if (retCode != RetCode.Success) {
            Log.e(TAG, "ADOSC Chart calc error retCode=" + retCode);
            return false;
        }

        mAdoscDateValueList = new ArrayList<>();
        for (int i = 0; i < dataLen; i++) {
            if (i < outBegIdx.value) {
                mAdoscDateValueList.add(new DateValueEntity(
                        (float) (outAdosc[0]), dates[i]));
            } else {
                int idx = i - outBegIdx.value;
                mAdoscDateValueList.add(new DateValueEntity(
                        (float) (outAdosc[idx]), dates[i]));
            }
        }

        List<LineEntity<DateValueEntity>> lines = new ArrayList<LineEntity<DateValueEntity>>();

        mAdoscLineEntity = new LineEntity<DateValueEntity>();
        mAdoscLineEntity.setTitle("ADOSC");
        mAdoscLineEntity.setLineColor(Color.WHITE);
        mAdoscLineEntity.setLineData(mAdoscDateValueList);
        lines.add(mAdoscLineEntity);

        mAdoscChartView.setLinesData(lines);

        mTaIdxDataSetsBegin  = outBegIdx.value;
        mTaIdxDataSetsNumber = outNBElement.value;
        return true;
    }

    @Override
    public int getTaIndexNumber() {
        return mTaIdxDataSetsNumber;
    }

    @Override
    public String getTaIndexParms() {
        return   mAdoscFastPeriod + ", " + mAdoscSlowPeriod;
    }

    @Override
    public String getTaIndexShortName() {
        return "ADOSC";
    }

    @Override
    public Spanned getTaIndexDetail() {
        if (null == mAdoscDateValueList) {
            return new SpannableString("----");
        }
        int index = mAdoscChartView.getSelectedIndex();

        DateValueEntity adoscEntity = mAdoscDateValueList.get(index);
        String          adoscText   = String.format(Locale.ENGLISH, "ADOSC:%.2f ",
                adoscEntity.getValue());
        String          dateText  = String.format(Locale.ENGLISH, "%d ", adoscEntity.getDate());

        String    finalString = adoscText + dateText;
        Spannable sb          = new SpannableString(finalString);

        sb.setSpan(new ForegroundColorSpan(mAdoscLineEntity.getLineColor()), finalString.indexOf(adoscText),
                finalString.indexOf(adoscText) + adoscText.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.GRAY), finalString.indexOf(dateText),
                finalString.indexOf(dateText) + dateText.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

        return (sb);
    }

    @Override
    public int getTaIndexBegin() {
        return mTaIdxDataSetsBegin;
    }

    @Override
    public View getChartView() {
        return mAdoscChartView;
    }

    @Override
    public void setDisplayFrom(int from) {
        mAdoscChartView.setDisplayFrom(from);
    }

    @Override
    public void setDisplayCrossXOnTouch(boolean displayCrossXOnTouch) {
        mAdoscChartView.setDisplayCrossXOnTouch(displayCrossXOnTouch);
    }

    @Override
    public void setDisplayNumber(int number) {
        mAdoscChartView.setDisplayNumber(number);
    }

    @Override
    public void setOnTouchListener(OnTouchListener l) {
        mAdoscChartView.setOnTouchListener(l);
    }

    @Override
    public void setOnDisplayCursorListener(
            IDisplayCursorListener l) {
        mAdoscChartView.setOnDisplayCursorListener(l);
    }

    @Override
    public PointF getTouchPoint() {
        return mAdoscChartView.getTouchPoint();
    }

    @Override
    public void setTouchPoint(PointF touchPoint) {
        mAdoscChartView.setTouchPoint(touchPoint);
    }

    @Override
    public void postInvalidate() {
        mAdoscChartView.postInvalidate();
    }

    @Override
    public float getValidMiddleY() {
        return mAdoscChartView.getValidMiddleY();
    }
}
